package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.qiantu.api.entity.RoomBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;

/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2019/09/22
 *    desc   : 房间标签数据列表
 */
public final class RoomOptionAdapter extends AppAdapter<RoomBean> {

    private RoomBean selected = null;
    public RoomOptionAdapter(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder();
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {

        private final TextView mTextView;

        private ViewHolder() {
            super(R.layout.attach_option_item);
            mTextView = findViewById(R.id.tv_option);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if(view.isSelected()){
                return;
            }
            view.setSelected(!view.isSelected());
            RoomBean data = (RoomBean) view.getTag();
            selected = data;
            notifyDataSetChanged();
            super.onClick(view);
        }

        @Override
        public void onBindView(int position) {
            RoomBean room = getItem(position);
            mTextView.setText(room.getRoomName());
            itemView.setTag(room);
            if(selected.equals(getItem(position))){
                itemView.setSelected(true);
                mTextView.setTypeface(Typeface.DEFAULT_BOLD);
            }else{
                itemView.setSelected(false);
                mTextView.setTypeface(Typeface.DEFAULT);
            }
        }
    }

    public RoomBean getSelected() {
        return selected;
    }

    public void checkSelected(RoomBean selected){
        if(selected != null){
            this.selected = selected;
            notifyDataSetChanged();
        }
    }

    public void setSelected(RoomBean selected){
        if(selected != null){
            this.selected = selected;
        }
    }
}